package com.xk.meeting.controller;

import com.xk.meeting.service.ApplyService;
import com.xk.meeting.service.ApplyServiceImpl;
import com.xk.meeting.utils.UtilHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Calendar;
import java.util.Date;

@WebServlet("/datetimeVer")
public class DatetimeVerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        String start = request.getParameter("start").replace("T"," ");
        String end = request.getParameter("end").replace("T"," ");
        String mid = request.getParameter("mid");



        ApplyService applyService = new ApplyServiceImpl();


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date1 = null;
        Date date2 = null;
        try {
            date1 = sdf.parse(start);
            date2 = sdf.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        /*System.out.println("------------------");
        System.out.println(date1);
        System.out.println(date2);*/


        long startDate = date1.getTime();
        long endDate = date2.getTime();



        int startHour = Integer.parseInt(start.substring(11,13));
        int endHour = Integer.parseInt(start.substring(11,13));


        if (startDate >= endDate){
            out.print("结束时间必须晚于开始时间");
        }else if(endDate - startDate >= 1000*60*60*14) {
            out.print("预约时常不可超出14小时");
        }else if(startHour < 8 || endHour >= 22){
            out.print("请在规定的时间段选择预约时间");
        }else if (applyService.isExistTime(start,end,mid)){
            out.print("此时间段已被预约,请换时间段");
        }else {
            out.print("ok");
        }
    }
}
